<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>显示隐藏密码明文</title>
    <style>
      .box {
        position: relative;
        width: 400px;
        margin: 100px auto 0;
        border-bottom: 1px solid #eeeeee;
      }

      .box > input {
        width: 364px;
        height: 30px;
        border: none;
        outline: none;
      }

      .box > .img-box {
        position: absolute;
        right: 0;
        top: 0;
        width: 32px;
        height: 32px;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <input type="password" />

      <div class="img-box">
        <img src="./imgs/close.png" />
      </div>
    </div>

    <script>
      var inputEle = document.querySelector('input');
      var imgEle = document.querySelector('img');
      var flag = 0;

      imgEle.onclick = function () {
        if (flag === 0) {
          inputEle.type = 'text';
          imgEle.src = './imgs/open.png';
          flag = 1;
        } else {
          inputEle.type = 'password';
          imgEle.src = './imgs/close.png';
          flag = 0;
        }
      };
    </script>
  </body>
</html>
